package com.yzx.lifeassistants.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.poi.PoiSortType;
import com.haarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.yzx.lifeassistants.R;
import com.yzx.lifeassistants.adapter.FoodInfoAdapter;
import com.yzx.lifeassistants.base.BaseActivity;
import com.yzx.lifeassistants.utils.ToastUtils;
import com.yzx.lifeassistants.view.widget.CircularLoadingDialog;

/**
 * @Description: 搜索美食结果界面
 * @author: yzx
 * @time: 2016-4-11 下午1:47:49
 */
public class FoodResultActivity extends BaseActivity implements
		OnClickListener, OnItemClickListener, OnGetPoiSearchResultListener {

	private ImageButton backBtn;// 返回按钮
	private ListView foodLV;// 讲座列表
	private PullToRefreshListView foodPTRLV;// 刷新
	private ILoadingLayout foodILL;// 加载

	private PoiSearch mPoiSearch;// POI搜索
	private int mPage;// 第几页
	private String mKeyWord;// 搜索关键词
	private int mRadius;// 半径距离
	private int mpageCapacity;// 每页数据量
	private LatLng mLatLng;// 定位结果

	private CircularLoadingDialog dialog;// 加载
	private static final int STATE_REFRESH = 0;// 下拉刷新
	private static final int STATE_MORE = 1;// 加载更多
	private int loadType;// 加载类型
	private List<PoiInfo> dataList;// 美食列表
	private FoodInfoAdapter adapter;// 美食列表适配器

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_food_result);
		init();
		initView();
		setListener();
		initData();
	}

	/**
	 * 
	 * @Description: 初始化
	 */
	private void init() {
		Intent intent = getIntent();
		if (null == intent) {
			mPage = 0;
			mKeyWord = "美食";
			mRadius = 500;
			mpageCapacity = 10;
		} else {
			mPage = intent.getIntExtra("page", 0);
			mKeyWord = intent.getStringExtra("keyWord");
			mRadius = intent.getIntExtra("radius", 500);
			mpageCapacity = intent.getIntExtra("pageCapacity", 10);
			mLatLng = intent.getParcelableExtra("location");
		}
	}

	/**
	 * 
	 * @Description: 初始化控件
	 */
	private void initView() {
		backBtn = (ImageButton) findViewById(R.id.food_result_back_btn);
		foodPTRLV = (PullToRefreshListView) findViewById(R.id.food_result_listview);
		foodILL = foodPTRLV.getLoadingLayoutProxy();
		foodILL.setLastUpdatedLabel("");
		foodILL.setPullLabel("继续拖动");
		foodILL.setRefreshingLabel("正在装载数据···");
		foodILL.setReleaseLabel("放开装载更多");
		foodLV = foodPTRLV.getRefreshableView();
	}

	/**
	 * 
	 * @Description: 设置监听
	 */
	private void setListener() {
		backBtn.setOnClickListener(this);
		// 滑动监听
		foodPTRLV.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				if (firstVisibleItem == 0) {
					foodILL.setLastUpdatedLabel("");
					foodILL.setPullLabel("下拉刷新");
					foodILL.setRefreshingLabel("加载中···");
					foodILL.setReleaseLabel("释放刷新");
				} else if (firstVisibleItem + visibleItemCount == totalItemCount) {
					foodILL.setLastUpdatedLabel("");
					foodILL.setPullLabel("继续拖动");
					foodILL.setRefreshingLabel("正在装载数据···");
					foodILL.setReleaseLabel("放开装载更多");
				}
			}
		});
		// 下拉刷新监听
		foodPTRLV.setOnRefreshListener(new OnRefreshListener2<ListView>() {

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				// 下拉刷新(从第一页开始装载数据)
				queryFood(0, STATE_REFRESH);
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				// 上拉加载更多(加载下一页数据)
				queryFood(mPage + 1, STATE_MORE);
			}

		});
		foodLV.setOnItemClickListener(this);
	}

	/**
	 * 
	 * @Description: 初始化数据
	 */
	private void initData() {
		dialog = new CircularLoadingDialog(this);
		dataList = new ArrayList<PoiInfo>();
		adapter = new FoodInfoAdapter(this, dataList);
		SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(
				adapter);
		swingBottomInAnimationAdapter.setAbsListView(foodLV);
		foodLV.setAdapter(swingBottomInAnimationAdapter);
		// 初始化搜索模块，注册搜索事件监听
		mPoiSearch = PoiSearch.newInstance();
		mPoiSearch.setOnGetPoiSearchResultListener(this);
		queryFood(mPage, STATE_MORE);
	}

	/**
	 * 
	 * @Description: 查询美食
	 */
	private void queryFood(int page, int actionType) {
		dialog.show();
		loadType = actionType;
		PoiNearbySearchOption newrOption = new PoiNearbySearchOption();
		newrOption.pageNum(page);
		newrOption.keyword(mKeyWord);
		newrOption.location(mLatLng);
		newrOption.pageCapacity(mpageCapacity);// 设置单页容量
		newrOption.radius(mRadius);
		newrOption.sortType(PoiSortType.comprehensive);
		mPoiSearch.searchNearby(newrOption);
	}

	/**
	 * 
	 * @Description: 按键监听
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.food_result_back_btn: {// 返回
			finish();
			break;
		}

		default:
			break;
		}
	}

	/**
	 * 
	 * @Description: 列表监听
	 */
	@Override
	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long arg3) {
		Intent intent = new Intent(this, FoodInfoActivity.class);
		intent.putExtra("poiInfo", dataList.get(position - 1));
		startActivity(intent);
	}

	/**
	 * 
	 * @Description: Toast显示信息
	 */
	private void showToast(String message, Boolean isInfo) {
		if (isInfo) {
			ToastUtils.showInfoToast(this, message, R.id.food_result_top_ll);
		} else {
			ToastUtils.showAlertToast(this, message, R.id.food_result_top_ll);
		}

	}

	/**
	 * 
	 * @Description: 搜索美食详情结果
	 */
	@Override
	public void onGetPoiDetailResult(PoiDetailResult result) {

	}

	/**
	 * 
	 * @Description: 搜索美食结果
	 */
	@Override
	public void onGetPoiResult(PoiResult result) {
		dialog.dismiss();
		foodPTRLV.onRefreshComplete();
		if (result == null
				|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
			if (loadType == STATE_REFRESH) {
				showToast("附近暂未找到美食~", true);
			} else {
				showToast("附近暂未找到更多美食~", true);
			}
			return;
		}
		if (result.error == SearchResult.ERRORNO.NO_ERROR) {
			if (loadType == STATE_REFRESH) {
				mPage = 0;
				dataList.clear();
			} else {
				mPage++;
			}
			dataList.addAll(result.getAllPoi());
			adapter.notifyDataSetChanged();
			return;
		}
		if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
			if (loadType == STATE_REFRESH) {
				showToast("附近暂未找到美食~", true);
			} else {
				showToast("附近暂未找到更多美食~", true);
			}
		}
	}
}
